/**
 * 
 */
package com.tw.trips.service;

import java.util.Collection;
import java.util.Date;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tw.trips.dao.LocationDAO;
import com.tw.trips.dao.UpdatePostDAO;
import com.tw.trips.domain.Activity;
import com.tw.trips.domain.Comment;
import com.tw.trips.domain.ItHelps;
import com.tw.trips.domain.Location;
import com.tw.trips.domain.Traveller;
import com.tw.trips.domain.Update;

/**
 * @author M1005264
 * 
 */
@Service
@Transactional
public class LocationService {

	@Autowired
	private LocationDAO locationDAO;

	@Autowired
	private UpdatePostDAO updatesDAO;
	
	public Set<Update> getUpdatesForLocation(Location reqLocation) {
		return locationDAO.getUpdates(reqLocation);
	}

	// TODO Check to enusre returns the Location with All the updates/Comments
	// for Updates and ITHelps on the Updates and Commnets
	public Location getLocation(String reqLocation) {
		return locationDAO.getLocation(reqLocation);
	}

	// TODO Verify
	public Location putUpdateForLocation(Location reqLocation,
			Traveller author, Update update) {
		author.addUpdate(update);
		reqLocation.addUpdate(update);
		Update retUpdate = updatesDAO.saveOrUpdate(update);
		return retUpdate.getForLocation();
	}

	// TODO Check to ensure Adds the ItHelps for the required Updates and
	// LOcation
	public Location addItHepsToUpdate(Update update, Traveller commentedBy) {
		ItHelps itHelps = new ItHelps(null, commentedBy, update);
		itHelps = updatesDAO.saveOrUpdate(itHelps);
		return itHelps.getUpdate().getForLocation();
	}

	// TODO Check to ensure Adds the ItHelps for the required Comments and
	// updated to the required Uodated and Lcoation
	public Location addItHepsToComment(Comment comment, Traveller commentedBy) {
		ItHelps itHelps = new ItHelps(comment, commentedBy, null);
		itHelps = updatesDAO.saveOrUpdate(itHelps);
		return itHelps.getComment().getUpdate().getForLocation();
	}

	public Traveller addItHelpsToActivity(Activity activity, Traveller addedBy) {
		ItHelps itHelps = new ItHelps(null, addedBy, null);
		itHelps.setActivity(activity);
		itHelps = updatesDAO.saveOrUpdate(itHelps);
		return itHelps.getActivity().getTrip().getCreatedBy();
	}

	public Collection<Location> getAllLocations() {
		return locationDAO.findAllLocations();
	}

	public Location getOrAddLocation(String locationFromCode) {
		Location location = getLocation(locationFromCode);
		if(location == null)
			location = locationDAO.save(new Location(locationFromCode));
		return location;
	}

	// TODO check if the stored Comment is fine and retrival for response
	// getUpdate().getForLocation() return the complete Location with updates
	// and other details
	public Location putCommentForUpdate(Update update, Date commentedOn,
			Traveller commentedBy, String comment) {
		// Comment cmnt = update.comment(operations, commentedBy, commentedOn,
		// comment);
		Comment cmnt = new Comment(commentedOn, commentedBy, comment);
		cmnt.setUpdate(update);
		cmnt = updatesDAO.saveOrUpdate(cmnt);
		return cmnt.getUpdate().getForLocation();
	}
}
